from CvPythonExtensions import *

import GerikSpells
from GerikSpells import GerikSpellBase
from GSGroupPolicies import GSAll as GSGroup
from GSHandlerPolicies import GSTargetting as GSHandler
from GSResultsPolicies import GSMission as GSResults

from GSTargetInfoPolicies import GSSinglePlotTarget
from GSTargetInfoPolicies import GSSpecificUnitTarget

from GSGraphicsPolicies import GSPlot
from GSGraphicsPolicies import GSPopup

gc = CyGlobalContext()

class SacrificialOath(GerikSpellBase, GSGroup, GSHandler, GSResults):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        

        # Create the list of stages
        stageList = []

        # First stage: get a plot.
        pPlotTargetPolicy = GSSinglePlotTarget(self.canTargetPlot)
        pPlotGraphicsPolicy = GSPlot()
        pPlotGraphicsPolicy.setAffectedPlotTexture(27)
        pPlotGraphicsPolicy.setUnaffectedPlotColor("COLOR_BLACK", 0.0) # Alpha at 0, don't show unaffected plot.
        stageList.append( (pPlotTargetPolicy, pPlotGraphicsPolicy) )

        # Second stage. Get a unit.
        pUnitTargetPolicy = GSSpecificUnitTarget(self.canTargetUnit)
        pUnitMenuPolicy = GSPopup()
        pUnitMenuPolicy.setShowPictures(False)
        pUnitMenuPolicy.setTitleText("TXT_KEY_SPELL_SACRIFICIAL_OATH")
        stageList.append( (pUnitTargetPolicy, pUnitMenuPolicy) )

        GSHandler.__init__(self, stageList)
        GSResults.__init__(self, pUnitTargetPolicy)

    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if (pCastingUnit.getUnitType() == gc.getInfoTypeForString("UNIT_WARRIOR")):
            return True

    def canTargetPlot(self, pCastingGroup, pCastingPlot, pPlot, bVisible):
        return pPlot.getNumUnits() > 0

    def canTargetUnit(self, pCastingGroup, pCastingPlot, pUnit, bVisible):
        return not pUnit.isInvisible(pCastingGroup.getTeam(), False)

    def startSpellResultsOnTarget(self, pCastingGroup, pTarget):
        CyEngine().triggerEffect(40, pTarget.plot().getPoint())
        CyEngine().triggerEffect(40, pCastingGroup.plot().getPoint())
        pTarget.kill(True, pCastingGroup.getOwner())
        pCastingGroup.getHeadUnit().kill(True, pCastingGroup.getOwner())
